/**
 * Created by xiongxin on 2017/11/15.
 */

 //import loadingImg from '../assets/image/loading_1.gif'

(function () {

  var defaultPx = 20;
  var disX, disY;
  var iH = document.documentElement.clientHeight || document.body.clientHeight;
  var iW = document.documentElement.clientWidth || document.body.clientWidth;
  var flag = false;
  var startX, startY, endX, endY, startMs, endMs;
  var oUl = null;
  var oDiv = null;

  function DBSDrag(id) {
    this.id = id;
    this.div = document.getElementById(id);
    oDiv = this.div;
    oUl = this.div.getElementsByTagName('ul')[0];

    this.init();
  }

  DBSDrag.prototype.init = function () {
    flag = false;
    var thisW = this.div.offsetWidth;

    this.div.onclick = dragToggle;

    var ul = this.div.getElementsByTagName('ul')[0];
    ul.style.left = thisW / 2 + 'px';
    ul.style.transformOrigin = 'left';
    ul.style.webkitTransformOrigin = 'left';

    this.div.addEventListener('touchstart', touchStart, false);
  };

  DBSDrag.prototype.reSetPosition = function () {
    this.div.style.left = defaultPx + 'px';
    this.div.style.top = '200px';
  };

  function dragToggle(state) {
    var ul = oUl;
    var div = oDiv;
    var obj = {};

    obj.open = function () {
      ul.style.transform = 'scaleX(1)';
      ul.style.WebkitTransform = 'scaleX(1)';
      ul.style.mozTransform = 'scaleX(1)';
      ul.style.oTransform = 'scaleX(1)';
      div.style.opacity = '1';
      flag = true;
    };
    obj.close = function () {
      ul.style.transform = 'scaleX(0)';
      ul.style.WebkitTransform = 'scaleX(0)';
      ul.style.mozTransform = 'scaleX(0)';
      ul.style.oTransform = 'scaleX(0)';
      flag = false;
    };

    if (typeof state === 'string') {
      obj[state]();
      return;
    }
    if (flag) {
      obj.close();
    } else {
      obj.open();
    }
  }

  function touchStart(e) {
    this.H = this.offsetHeight;
    this.W = this.offsetWidth;
    this.style.opacity = '1';
    //this.style.transition = '0s all ease-in';
    //this.style.webkitTransition = '0s all ease-in';
    startX = e.changedTouches[0].pageX;
    startY = e.changedTouches[0].pageY;
    startMs = new Date().getTime();

    disX = startX - this.offsetLeft;
    disY = startY - this.offsetTop;

    this.addEventListener('touchmove', touchMove, false);
    this.addEventListener('touchend', touchEnd, false);
    e.stopPropagation();
  }

  function touchMove(e) {

    var ul = this.getElementsByTagName('ul')[0];
    if (flag) {
      dragToggle('close');
    }

    var l = e.changedTouches[0].pageX - disX;
    var t = e.changedTouches[0].pageY - disY;
    var thisH = this.offsetHeight;
    var thisW = this.offsetWidth;

    if (l < defaultPx) l = defaultPx;
    if (l > iW - thisW - defaultPx) l = iW - thisW - defaultPx;
    if (t < defaultPx) t = defaultPx;
    if (t > iH - thisH - defaultPx) t = iH - thisH - defaultPx;

    this.style.left = l + 'px';
    this.style.top = t + 'px';

    if (t == defaultPx && e.changedTouches[0].pageY < defaultPx) {
      touchEnd.call(this, e);
    }
    e.stopPropagation();
    e.preventDefault();
  }

  function touchEnd(e) {
    var ul = this.getElementsByTagName('ul')[0];
    var thisL = this.offsetLeft;
    var thisW = this.offsetWidth;
    var l;

    this.style.opacity = '0.5';
    endX = e.changedTouches[0].pageX;
    endY = e.changedTouches[0].pageY;
    endMs = new Date().getTime();

    if (thisL < iW / 2) {
      l = defaultPx;
    } else {
      l = iW - this.W - defaultPx;
    }

    if (this.offsetLeft < iW / 2) {
      ul.style.transformOrigin = 'left';
      ul.style.webkitTransformOrigin = 'left';
      setTimeout(function () {
        ul.style.left = thisW / 2 + 'px';
      }, 300);
      ul.style.borderRadius = '0 30px 30px 0';
    } else {
      ul.style.transformOrigin = 'right';
      ul.style.webkitTransformOrigin = 'right';
      setTimeout(function () {
        ul.style.left = -(ul.offsetWidth - thisW / 2) + 'px';
      }, 300);
      ul.style.borderRadius = '30px 0 0 30px';
    }

    //this.style.transition = '0.1s all ease-in';
    //this.style.webkitTransition = '0.1s all ease-in';
    this.style.left = l + 'px';
    this.removeEventListener('touchmove', touchMove, false);
    this.removeEventListener('touchend', touchEnd, false);

    e.stopPropagation();
  }

  window.DBSDrag = DBSDrag;
  window.dragToggle = dragToggle;
})();



(function () {

  //利用sessionStorage开放一个key来存取json格式的字符串
  var key = '_dbs_ram';

  var Local = {
    set: function (name, value) {
      this.userInfo[name] = value;
      sessionStorage[key] = JSON.stringify(this.userInfo);
    },

    get: function (name) {
      return this.userInfo[name];
    },

    remove: function (name) {
      delete this.userInfo[name];
      sessionStorage[key] = JSON.stringify(this.userInfo);
    },

    clear: function () {
      sessionStorage.removeItem(key);
    },

    userInfo: (function () {
      if (typeof sessionStorage[key] !== 'undefined') {
        return JSON.parse(sessionStorage[key]);
      } else {
        return {};
      }
    })()
  };

  // // 加密
  // function Encrypt(word) {
  //   var key = CryptoJS.enc.Utf8.parse("123654123654");
  //   var iv = CryptoJS.enc.Utf8.parse("123654123654");
  //   var srcs = CryptoJS.enc.Utf8.parse(word);
  //   var encrypted = CryptoJS.AES.encrypt(srcs, key, {iv: iv, mode: CryptoJS.mode.CBC});
  //   return encrypted.toString();
  // }
  //
  // // 解密
  // function Decrypt(word) {
  //   var key = CryptoJS.enc.Utf8.parse("123654123654");
  //   var iv = CryptoJS.enc.Utf8.parse("123654123654");
  //   var decrypt = CryptoJS.AES.decrypt(word, key, {iv: iv, mode: CryptoJS.mode.CBC});
  //   return CryptoJS.enc.Utf8.stringify(decrypt).toString();
  // }

  window.Local = Local;
})();

(function () {

  //计数的背景幕
  var bgColor = 'rgba(0,0,0,0.7)';
  var XeLoading = {
    sum: 0,
    show: function () {
      this.sum++;
      if (this.sum == 1) {
        this.append();
      }
    },
    hide: function () {
      if (this.sum > 0) {
        this.sum--;
      }

      if (this.sum == 0) {
        this.remove();
      }
    },
    append: (function () {
      var body = document.getElementsByTagName('body')[0];
      var loadDiv = document.createElement('div');

      loadDiv.id = 'Xe___Loading';
      loadDiv.style.position = 'fixed';
      loadDiv.style.width = '100%';
      loadDiv.style.height = '100%';
      loadDiv.style.left = '0px';
      loadDiv.style.right = '0px';
      loadDiv.style.bottom = '0px';
      loadDiv.style.top = '0px';
      loadDiv.style.backgroundColor = bgColor;
      loadDiv.style.zIndex = '1001';

      return function () {
        body.appendChild(loadDiv);
      }
    })(),
    remove: function () {
      var body = document.getElementsByTagName('body')[0];
      var loadDiv = document.getElementById('Xe___Loading');

      if (loadDiv) body.removeChild(loadDiv);
    },
    clear: function () {
      this.sum = 0;
      this.hide();
    }
  };

  window.bgPop = XeLoading;
})();

//loading请求加载图片

(function () {
  var bgColor = 'rgba(0,0,0,0.5)';
  var loadPop = {};

  function extend(o, k) {
    for (var x in k) {
      o[x] = k[x];
    }
    return o;
  }

  loadPop = extend(loadPop, bgPop);

  loadPop.append = (function () {

    var body = document.getElementsByTagName('body')[0];
    var loadDiv = document.createElement('div');
    var imgDiv = document.createElement('div');
    var oImg = document.createElement('img');
    //oImg.src = 'https://cloud.bankofchina.com/cq/xinyi/static/xinyiApp/img/loading.gif';
    //oImg.src = loadingImg;
    oImg.src = './static/image/loading_1.gif';
    //oImg.src = '../../static/image/loading.gif';
    oImg.style.width = '1.5rem';
    //imgDiv.className = 'dbs_load_img';
    /*loading加载图*/
    imgDiv.style.position = 'absolute';
    imgDiv.style.top = '50%';
    imgDiv.style.transform = 'translateY(-50%)';
    imgDiv.style.width = '100%';
    imgDiv.style.textAlign = 'center';

    loadDiv.appendChild(imgDiv);
    imgDiv.appendChild(oImg);

    loadDiv.id = 'Xe___Loading2';
    loadDiv.style.position = 'fixed';
    loadDiv.style.left = '0px';
    loadDiv.style.right = '0px';
    loadDiv.style.bottom = '0px';
    loadDiv.style.top = '0px';
    loadDiv.style.zIndex = '10000';
    loadDiv.style.backgroundColor = bgColor;


    //oImg.style.textAlign = 'center';

    return function () {
      body.appendChild(loadDiv);
    }
  })();
  loadPop.remove = function () {
    var body = document.getElementsByTagName('body')[0];
    var loadDiv = document.getElementById('Xe___Loading2');

    if (loadDiv) body.removeChild(loadDiv);
  };

  window.loadPop = loadPop;
})();

(function () {
  var body = document.getElementsByTagName('body')[0];
  var oDiv = document.createElement('div');
  var p = document.createElement('p');
  var oDiv2 = document.createElement('div');
  var oDiv2Child = document.createElement('div');   //创建oDiv2的子对象
  var btnDiv = document.createElement('div');
  var flag = false;

  oDiv.style.zIndex = '10000';
  oDiv.style.position = 'absolute';
  oDiv.style.width = '2.27rem';
  oDiv.style.height = '1.33rem';
  oDiv.style.backgroundColor = 'white';
  oDiv.style.left = 0;
  oDiv.style.top = 0;
  oDiv.style.bottom = 0;
  oDiv.style.right = 0;
  oDiv.style.margin = 'auto';
  oDiv.style.borderRadius = '0.05rem';
  oDiv.style.overflow = 'hidden';
  oDiv.style.backgroundClip = 'padding-box';

  p.style.textAlign = 'center';
  p.style.fontSize = '0.18rem';
  p.style.height = '0.34rem';
  p.style.lineHeight = '0.34rem';
  p.style.borderBottom = '1px solid #dedfe0';
  p.style.color = '#333';
  p.style.margin = 0;


  oDiv2.style.wordBreak = 'break-word';
  oDiv2.style.minHeight = '0.6rem';
  oDiv2.style.overflow = 'hidden';
  oDiv2.style.fontSize = '0.13rem';
  oDiv2.style.textAlign = 'center';
  //oDiv2.style.padding = '0.1rem';
  oDiv2.style.display = 'table';
  oDiv2.style.color = '#8e8e8e';
  oDiv2.style.width = '100%';
  oDiv2.className = 'dbsDiv';

  oDiv2Child.style.display = 'table-cell';
  oDiv2Child.style.verticalAlign = 'middle';
  oDiv2Child.style.padding = '0 0.05rem';



  btnDiv.style.width = '100%';
  btnDiv.style.textAlign = 'center';
  btnDiv.style.marginBottom = '0.18rem';
  btnDiv.className = 'dbsBtnDiv';

  oDiv.appendChild(p);
  oDiv.appendChild(oDiv2);
  oDiv2.appendChild(oDiv2Child);
  oDiv.appendChild(btnDiv);
  var DbsAlert = {
    show: function (o) {
      var options = {
        text: '出错啦',
        onTap: null,
        title: '温馨提示',
        btnText: '确定',
        onClear: null
      };
      var that = this;
      if (typeof o === 'string') {
        options.text = o;
      } else {
        options = extend(options, o);
      }

      p.innerText = options.title;
      // oDiv2.innerText = options.text;
      oDiv2Child.innerText = options.text;


      if (!options.clearBtnText) {
        btnDiv.innerHTML = '<button style="width:1.78rem;height:0.24rem;border:1px solid #ff4444;color: #ff4444;font-size:0.13rem;background-color:white;border-radius:0.02rem;">' + options.btnText + '</button>';
      } else {
        btnDiv.innerHTML = '<button class="dbs_red fl" style="background-color:#8e8e8e;color:#fff;border:none;width:0.69rem;height:0.24rem;font-size:0.13rem;border-radius:0.02rem;margin-right: 0.3rem">' + options.clearBtnText + '</button>' +
          '<button class="dbs_red fr" style="background-color:#ff4444;color:#fff;border:none;width:0.69rem;height:0.24rem;font-size:0.13rem;border-radius:0.02rem;">' + options.btnText + '</button>';

      }

      var clearBtn = btnDiv.getElementsByTagName('button')[0];
      var confirmBtn = btnDiv.getElementsByTagName('button')[1];

      clearBtn.onclick = function () {
        if (options.onClear) options.onClear();
        that.hide();
      };

      if (confirmBtn) {
        confirmBtn.onclick = function () {
          if (options.onTap) options.onTap();
          that.hide();
        };
      }

      body.appendChild(oDiv);
      this.div = oDiv;
      if (!flag) {
        flag = true;
        bgPop.show();
      }
    },

    hide: function () {
      if (flag) {
        flag = false;
        body.removeChild(oDiv);
        bgPop.hide();
      }
    }
  };

  function extend(a, b) {
    for (var i in b) {
      a[i] = b[i];
    }
    return a;
  }

  window.DbsAlert = DbsAlert;
})();


// (function () {
//   /**
//    * number:为需要格式化的数值
//    * cent： 为number要保留的小数位
//    * isThousand： 为是否需要千分位 0:不需要,1:需要(数值类型);
//    * 方法返回结果为格式的字符串,如'1,234,567.00'
//    */
//
//   function formatNumber(number) {
//     number = number.toString().replace(/\$|\,/g, '');
//
//     // 1、检查传入数值为数值类型
//     if (isNaN(number))
//       number = '0';
//     // 2、获取符号(正/负数)
//     sign = (number == (number = Math.abs(number)));//Math.abs(number)取出number的绝对值
//
//     // 3、把指定的小数位先转换成整数.多余的小数位四舍五入，Math.pow(10, cent)为10的cent次方
//     number = Math.floor(number * Math.pow(10, 2) + 0.5);
//     // 4、求出小数位数值
//     cents = number % Math.pow(10, 2);
//     // 5、求出整数位数值
//     number = Math.floor(number / Math.pow(10, 2)).toString();
//     // 6、把小数位转换成字符串,以便求小数位长度
//     cents = cents.toString();
//
//     // 7、补足小数位到指定的位数
//     while (cents.length < 2)
//       cents = "0" + cents;
//     // 8、对整数部分进行千分位格式化.
//     for (var i = 0; i < Math.floor((number.length - (1 + i)) / 3) ; i++)
//       number = number.substring(0, number.length - (4 * i + 3)) + ',' + number.substring(number.length - (4 * i + 3));
//     //9、返回格式化转换后的结果
//     return (((sign) ? '' : '-') + '￥' + number + '.' + cents);
//   }
//   window.formatNumber = formatNumber;
// })();




function getUrl() {
  if(window.location.href){
    var url = window.location.href.split('?')[1];
    if(url){
      var arr = url.split('&');
      var obj = {};
      for (var i=0;i<arr.length;i++){
        obj[arr[i].split('=')[0]] = arr[i].split('=')[1];
      }
      return obj;
    }
  }
}

function convertToChinese(num) {
  num = Number(num);
  var upperCaseNumber = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '百', '千', '万', '亿'];
  var length = String(num).length;
  if (length == 1) {
    return upperCaseNumber[num];
  } else if (length == 2) {
    if (num == 10) {
      return upperCaseNumber[num];
    } else if (num > 10 && num < 20) {
      return '十' + upperCaseNumber[String(num).charAt(1)];
    } else {
      return upperCaseNumber[String(num).charAt(0)] + '十' + upperCaseNumber[String(num).charAt(1)].replace('零', '');
    }
  }
}

// function format(fmt) {
//   Date.prototype.Format = function (fmt) { //author: meizz
//     var o = {
//       "M+": this.getMonth() + 1, //month
//       "d+": this.getDate(), //day
//       "h+": this.getHours(), //hour
//       "m+": this.getMinutes(), //minute
//       "s+": this.getSeconds(), //second
//       "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
//       "S": this.getMilliseconds() //millisecond
//     };
//     if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
//     for (var k in o)
//       if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
//     return fmt;
//   };
// }

(function () {
  Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
      "M+": this.getMonth() + 1, //month
      "d+": this.getDate(), //day
      "h+": this.getHours(), //hour
      "m+": this.getMinutes(), //minute
      "s+": this.getSeconds(), //second
      "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
      "S": this.getMilliseconds() //millisecond
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  };
})();

const comJs = {
  getUrl,
  convertToChinese
}

export default comJs;
